package hw;

import sk.Util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TransPanel extends JPanel implements ActionListener {
    private ATM atm;
    private JButton sureBtn;
    private JButton clearBtn;
    private Icon sureImg = Util.getImageIcon("/images/buttonSure.jpg");
    private Icon clearImg = Util.getImageIcon("/images/buttonOff.jpg");

    private JTextField transname = new JTextField();
    private JTextField transmoney = new JTextField();

    public TransPanel(ATM atm) {
        this.atm = atm;
        this.setLayout(null);
        this.setOpaque(false);

        JLabel namelable = new JLabel("请输入转账人姓名：");
        namelable.setBounds(200, 200, 150, 30);
        namelable.setFont(new Font("黑体", Font.BOLD, 16));
        this.add(namelable);

        transname.setBounds(370, 200, 100, 30);
        transname.setFont(new Font("黑体", Font.BOLD, 16));
        this.add(transname);

        JLabel moneylable = new JLabel("请输入转账金额：");
        moneylable.setBounds(200, 250, 150, 30);
        moneylable.setFont(new Font("黑体", Font.BOLD, 16));
        this.add(moneylable);

        transmoney.setBounds(370, 250, 100, 30);
        transmoney.setFont(new Font("黑体", Font.BOLD, 16));
        this.add(transmoney);

        sureBtn = new JButton(sureImg);
        sureBtn.setBounds(200, 300, 140, 50);
        this.add(sureBtn);
        sureBtn.addActionListener(this);

        clearBtn = new JButton(clearImg);
        clearBtn.setBounds(420, 300, 140, 50);
        this.add(clearBtn);
        clearBtn.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
//        if (e.getSource() == sureBtn) {
//            atm.switchPanel("menu");
//        } else if (e.getSource() == clearBtn) {
//            atm.switchPanel("login");
//        }

        if (e.getSource() == sureBtn) {
            if (transname.getText().equals(atm.getUsers().get(atm.getNum()).getUsername())) {
                JOptionPane.showMessageDialog(this, "不能转账给自己");
                return;
            }
            for (int i = 0; i < atm.getUsers().size(); i++) {
                if (transname.getText().equals(atm.getUsers().get(i).getUsername())) {
                    if (Double.parseDouble(transmoney.getText()) > atm.getUsers().get(atm.getNum()).getMoney()) {
                        JOptionPane.showMessageDialog(this, "钱不够，请重新输入");
                        return;
                    }
                    JOptionPane.showMessageDialog(this, "转账金额为：" + transmoney.getText() + ",余额为" + (atm.getUsers().get(atm.getNum()).getMoney() - Double.parseDouble(transmoney.getText())));
                    atm.getUsers().get(atm.getNum()).setMoney((atm.getUsers().get(atm.getNum()).getMoney()) - Double.parseDouble(transmoney.getText()));
                    atm.getUsers().get(i).setMoney(atm.getUsers().get(i).getMoney() + Double.parseDouble(transmoney.getText()));
                    atm.switchPanel("menu");
                    return;
                }

            }

            JOptionPane.showMessageDialog(this, "查无此人");

        } else if (e.getSource() == clearBtn) {
            atm.switchPanel("login");
        }


    }
}
